from datetime import date, datetime
from mailbox import Message
from typing import Literal

from django.shortcuts import get_object_or_404
from ninja import Form, Router, Schema
from psycopg2 import Error

from call.models import Example
from call.views import get_ariadna
from customauth.api import StatusMessage
from decorators.auth_login import login_for_logs_by_mpp

import pg

router = Router(tags=["call"])


@router.get("/hello")
def hello(request) -> Literal["Hello world"]:  # noqa: ARG001
    return "Hello world"


class ExampleSchema(Schema):
    id: int = None
    name: str


@router.get("/hello_get_message/", response=list[ExampleSchema])
def  hello_get_message(request) -> list[ExampleSchema]:  # noqa: ARG001
    return Example.objects.all()


class ExampleSchemaInput(Schema):
    name: str
    mpp: int


@router.post("/hello_post_message/")
@login_for_logs_by_mpp
def  hello_post_message(request, example_in: ExampleSchemaInput) -> list[ExampleSchema]:  # noqa: ARG001
    Example.objects.create(name=example_in.name)
    return 200


class ExampleSchemaDeleteIn(Schema):
    example_id: int
    mpp: int

@router.delete("/hello_delete_message/")
@login_for_logs_by_mpp
def  hello_delete_message(request, add_example: ExampleSchemaDeleteIn) -> Literal[200]:  # noqa: ARG001
    example = get_object_or_404(Example, id=add_example.example_id)
    example.delete()
    return 200

class DataOutSchema(Schema):
    dd: str
    updatedate: str
    dep_text: str
    spec_text: str
    doc: str
    num: int
    lastname: str
    firstname: str
    secondname: str | None
    birthdate : str
    cellular: str | None
    note: str | None
    keyid: int

@router.get("/get_from_ariadna/", response=list[DataOutSchema])
def get_from_ariadna(request):
    data = get_ariadna()
    # print(data)
    return data

class DataUpdSchema(Schema):
    updnote: str
    keyid: int

@router.post("/upd_rnumb_note/")
def upd_rnumb_note(request, updRnumbNote:DataUpdSchema)-> Literal[200]:
    try:
        pg.write(f"update rnumb set note='{updRnumbNote.updnote}' where keyid={updRnumbNote.keyid}")
    except Exception:  # noqa: BLE001
        return 403, {"message": "Error add comment"}
    return 200, {"ok": True, "status": 200, "message": "add comment completed"}